package com.csxy.dyq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.dyq.R;
import com.csxy.dyq.entity.BannerQuery;
import com.csxy.dyq.entity.CrmBanner;
import com.csxy.dyq.service.CrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cmsservice/banner")
public class CrmBannerController {
    @Autowired
    private CrmService crmService;
    // 条件分页查询banner
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit,
                        @RequestBody(required = false) BannerQuery bannerQuery) {
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        crmService.pageQuery(bannerPage, bannerQuery);
        // 获取数据
        List<CrmBanner> list = bannerPage.getRecords();
        // 获取总记录数
        long total = bannerPage.getTotal();
        return R.ok().data("rows", list).data("total", total);
    }
    //    增加
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        boolean save = crmService.save(crmBanner);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //    更新
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean update = crmService.updateById(crmBanner);
        if (update){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //    删除
    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBanner(@PathVariable("id") String id) {
        boolean delete = crmService.removeById(id);
        if (delete){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //    根据ID查询Banner
    @GetMapping("/getBannerById/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner banner = crmService.getById(id);
        return R.ok().data("item", banner);
    }

}

